package com.example.hotel_demo.controller;


import com.example.hotel_demo.pojo.Customer;
import com.example.hotel_demo.pojo.Page;
import com.example.hotel_demo.pojo.RoomType;
import com.example.hotel_demo.pojo.User;
import com.example.hotel_demo.service.CustomerService;
import com.example.hotel_demo.service.RoomTypeService;
import com.example.hotel_demo.service.UserService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@ComponentScan
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    CustomerService customerService;
    @Autowired
    RoomTypeService roomTypeService;

    @Transactional
    @RequestMapping("/admin-userGuanli")
    public String userGuanli(Model model,String currentPage){
        Page pageUsers = userService.pageUsers(currentPage);
        List<User> userList = pageUsers.getShowUsers();
        model.addAttribute("userList",userList);
        model.addAttribute("pageUsers",pageUsers);
        return "/admin/admin-userGuanli";
    }
    @Transactional
    @RequestMapping("/admin-userGuanli-get")
    public String userGuanliget(Model model,String email){
        List<User> userList =userService.getUserALLByEmail(email);
        model.addAttribute("userList",userList);
        return "/admin/admin-userGuanli-get";
    }

    @RequestMapping("/admin-userGuanli-qiyong-tj")
    @ResponseBody
    public String updateUserqiyong(String uID){
        //启动，弹个comfig框
        return "<script type='text/javascript'> var flag=  " +
                "window.confirm('确认启用该用户吗');" +
                "if(flag == true)  " +
                "window.location='/admin-userGuanli-qiyong-tj-queren?uid="+ uID +
                "';else  " +
                "  window.location='/admin-userGuanli?currentPage=1' ;</script>";
    }
    @Transactional
    @RequestMapping("/admin-userGuanli-qiyong-tj-queren")
    @ResponseBody
    public String updateUserqiyongqueren(String uid){
        int flag = userService.updateUserStateByuid(uid,"0");
        if (flag ==1)
            return "<script type='text/javascript'>alert('启用成功');window.location='/admin-userGuanli?currentPage=1'</script>";
        else
            return "<script type='text/javascript'>alert('启用失败');window.location='/admin-userGuanli?currentPage=1'</script>";
    }

    @RequestMapping("/admin-userGuanli-tingyong-tj")
    @ResponseBody
    public String updateUsertingyong(String uID){
        //启动，弹个comfig框
        System.out.println(uID);
        return "<script type='text/javascript'> var flag=  " +
                "window.confirm('确认停用该用户吗');" +
                "if(flag == true)  " +
                "window.location='/admin-userGuanli-tingyong-tj-queren?uid="+ uID +
                "';else  " +
                "  window.location='/admin-userGuanli?currentPage=1' ;</script>";
    }

    @Transactional
    @RequestMapping("/admin-userGuanli-tingyong-tj-queren")
    @ResponseBody
    public String updateUsertingyongqueren(String uid){
        System.out.println(uid);
        int flag = userService.updateUserStateByuid(uid,"1");
        if (flag == 1)
            return "<script type='text/javascript'>alert('停用成功');window.location='/admin-userGuanli?currentPage=1'</script>";
        else
            return "<script type='text/javascript'>alert('停用失败');window.location='/admin-userGuanli?currentPage=1'</script>";
    }


    @RequestMapping("/admin-userGuanli-cust-tj")
    @ResponseBody
    public String updateUsergetcust(String uID){
        //启动，弹个comfig框
        return "<script type='text/javascript'> var flag=  " +
                "window.confirm('确认编辑该用户登记信息吗');" +
                "if(flag == true)  " +
                "window.location='/admin-userGuanli-cust-tj-queren?uid="+ uID +
                "';else  " +
                "  window.location='/admin-userGuanli?currentPage=1' ;</script>";
    }

    @Transactional
    @RequestMapping("/admin-userGuanli-cust-tj-queren")
    public String updateUsergetcustqueren(Model model,String uid){
        //进入修改顾客页面
        Customer customer = customerService.getCustByUid(uid);
        model.addAttribute("cust",customer);
        return "/admin/admin-userGuanli-cust";
    }

    @Transactional
    @RequestMapping("/admin-userGuanli-cust-chuli")
    @ResponseBody
    public String updateUserforcust(String custID,String custName,String custNum,String custPhone,String sex){
        //处理修改顾客的信息
        int flag = customerService.updateCustByCustID(custID,custName,custNum,custPhone,sex);
        if (flag ==1)
            return "<script type='text/javascript'>alert('修改顾客信息成功');window.location='/admin-userGuanli?currentPage=1'</script>";
        else
            return "<script type='text/javascript'>alert('修改失败');window.location='/admin-userGuanli?currentPage=1'</script>";
    }

    @RequestMapping("/admin-userGuanli-update-tj")
    @ResponseBody
    public String updateUser(String uID){
        //启动，弹个comfig框
        return "<script type='text/javascript'> var flag=  " +
                "window.confirm('确认编辑该用户登录信息吗');" +
                "if(flag == true)  " +
                "window.location='/admin-userGuanli-update-tj-queren?uid="+ uID +
                "';else  " +
                "  window.location='/admin-userGuanli?currentPage=1' ;</script>";
    }

    @Transactional
    @RequestMapping("/admin-userGuanli-update-tj-queren")
    public String updateUserqueren(Model model,String uid){
        //进入修改用户页面
        User user = userService.getUserByUid(uid);
        model.addAttribute("user",user);
        return "/admin/admin-userGuanli-update";
    }

    @Transactional
    @RequestMapping("/admin-userGuanli-update-chuli")
    @ResponseBody
    public String updateUserchuli(String uID,String username,String password,String repassword ){
        //处理修改用户的信息
        if(password == repassword) {
            int flag = userService.updateUserByUid(uID,username,password);
            if (flag ==1)
                return "<script type='text/javascript'>alert('修改顾客信息成功');window.location='/admin-userGuanli?currentPage=1'</script>";
            else
                return "<script type='text/javascript'>alert('修改失败');window.location='/admin-userGuanli?currentPage=1'</script>";
        }else {
            return "<script type='text/javascript'>alert('两次密码错误，请重新输入');" +
                    "window.location='/admin-userGuanli-update-tj-queren?uid="+uID+"'</script>";
        }
    }

    @RequestMapping("/admin-userGuanli-delete-tj")
    @ResponseBody
    public String delteUser(String uID){
        //启动，弹个comfig框
        return "<script type='text/javascript'> var flag=  " +
                "window.confirm('确认删除该用户登录信息吗');" +
                "if(flag == true)  " +
                "window.location='/admin-userGuanli-delete-chuli?uid="+ uID +
                "';else  " +
                "  window.location='/admin-userGuanli?currentPage=1' ;</script>";
    }

    @Transactional
    @RequestMapping("/admin-userGuanli-delete-chuli")
    @ResponseBody
    public String delteUserchuli(String uid){
        //删除用户的信息，级联删除，顾客表的uid变成空
        //需要开启事务
        int flag = userService.deleteUser(Integer.parseInt(uid));
        int flag2 = customerService.updateCustUidByuid(uid);
        if (flag ==1 && flag2==1)
            return "<script type='text/javascript'>alert('删除用户成功');window.location='/admin-userGuanli?currentPage=1'</script>";
        else
            return "<script type='text/javascript'>alert('删除失败');window.location='/admin-userGuanli?currentPage=1'</script>";

    }







    @RequestMapping("/admin")
    public String getadmin() {
        return "/admin/admin";
    }


    /**
     * 获取所有管理员 API(Restful风格)
     *
     * @return
     */
    @GetMapping("/admins")
    @ResponseBody
    @CrossOrigin
    public List<User> getAdminList() {
        return userService.getUserByRole(0);
    }

    /**
     * 增加管理员 API (Restful风格)
     * 返回JSON，增加成功就返回{"result":1},失败就返回{"result":-1}
     *
     * @param user
     * @return
     */
    @Transactional
    @PostMapping("/admins")
    @ResponseBody
    @CrossOrigin
    public String addAdmin(@RequestBody User user) {
        userService.registUser(user);
        int uId;
        user = userService.getUserByUsername(user.getUsername());
        if (user != null) {
            uId = user.getuID();
        } else {
            System.out.println("添加用户 : 找不到该用户");
            uId = -1;
        }

        return "{\"result\":"+(uId!=-1?1:-1)+","
                + "\"uId\":"
                + uId
                + "}";
    }

    /**
     * 删除管理员 API (Restful风格)
     * 返回JSON，增加成功就返回{"result":1},失败就返回{"result":-1}
     *
     * @param uID
     * @return
     */
    @Transactional
    @DeleteMapping("/admins/{uID}")
    @ResponseBody
    @CrossOrigin
    public String deleteAdmin(@PathVariable int uID) {
        int res=userService.deleteUser(uID);

        return "{\"result\":"+res+"}";
    }

    /**
     * 更新管理员 API (Restful风格)
     * 返回JSON，增加成功就返回{"result":1},失败就返回{"result":-1}
     *
     * @param user
     * @return
     */
    @Transactional
    @PutMapping("/admins/{uID}")
    @ResponseBody
    @CrossOrigin
    public String updateAdmin(@RequestBody User user) {
        int res=userService.updateUser(user);

        return "{\"result\":"+res+"}";
    }

    /**
     * 返回admin管理页面
     *
     * @return
     */
    @GetMapping("/admin-list")
    public String adminList() {
        return "/admin/admin-list";
    }
}
